<template>
  <div>
    <el-dialog
      :title="title + '流程'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="名称" prop="name">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="业务范围" prop="extFiled">
            <ExtFiledSelect
              v-if="dialogVisible"
              :defaultValue="form.extFiled"
              @emit="setExtFiled"
              :disabled="addOrEdit != 1"
            />
          </el-form-item>
          <!-- <el-form-item label="校区范围" prop="scope">
            <SchoolSelect
              v-if="dialogVisible"
              :defaultValue="form.scope"
              @emit="setScool"
              :disabled="addOrEdit != 1"
            />
          </el-form-item> -->
          <el-form-item label="排序">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="描述">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入描述"
              v-model="form.des"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  flowDefinitionAdd,
  flowDefinitionUpdate,
} from "@/api/admin/audit/flowDefinition.js";
import Bus from "@/utils/bus.js";
export default {
  components: {
    ExtFiledSelect: () => import("@/components/dataSelect/extFiledSelect"),
    SchoolSelect: () => import("@/components/dataSelect/schoolSelect"),
  },
  data() {
    return {
      title: "新增",
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        name: "",
        scope: "",
        extFiled: "",
        orderBy: 10,
        status: "0",
        des: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        extFiled: [
          { required: true, message: "请选择业务范围", trigger: "blur" },
        ],
        scope: [{ required: true, message: "请选择校区", trigger: "blur" }],
      },
    };
  },
  watch: {
    scope(v) {
      this.form.scope = v;
    },
  },
  props: ["scope"],
  mounted() {
    Bus.$on("showDialogFlowAdd", (v) => {
      if (v) {
        this.checkedData = v;
        this.handleAddOrEdit(2);
      } else {
        this.handleAddOrEdit(1);
      }
    });
  },
  beforeDestroy() {
    Bus.$off("showDialogFlowAdd");
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = "编辑";
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || "";
          }
        }
        this.form.id = this.checkedData.id;
      } else {
        this.title = "新增";
        delete this.form.id;
      }
      this.addOrEdit = type;
      this.dialogVisible = true;
    },
    submitForm(form) {
      this.form.scope = this.scope;
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit();
        }
      });
    },
    //新增
    add() {
      flowDefinitionAdd(this.form).then((res) => {
        if (!res || res.data.code != 0) return;
        this.dialogVisible = false;
        Bus.$emit("refreshFlowList");
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
    //编辑
    edit() {
      flowDefinitionUpdate(this.form).then((res) => {
        if (!res || res.data.code != 0) return;
        this.dialogVisible = false;
        Bus.$emit("refreshFlowList");
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
    setExtFiled(v) {
      this.form.extFiled = v;
    },
    setScool(v) {
      this.form.scope = v;
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields();
      this.form.name = "";
      this.form.extFiled = "";
      this.form.status = "0";
      this.form.orderBy = 10;
      this.form.des = "";
      this.form.scope = "";
    },
  },
};
</script>